<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登陆页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <link rel="stylesheet" href="css/components.css">

</head>

<body>
<div class="nav">
    <div class="logo-container">
        <div class="logo-icon">B</div>
        <span class="blog-title">我的博客系统</span>
    </div>
    <div class="space"></div>
    <a class="nav-span" href="blog_list.html">主页</a>
    <a class="nav-span" href="blog_edit.html">写博客</a>
</div>

<div class="container-login">
    <div class="login-dialog">
        <h3>登陆</h3>
        <div class="row">
            <span>用户名</span>
            <input type="text" name="username" id="username" placeholder="请输入用户名" autocomplete="username">
        </div>
        <div class="row">
            <span>密码</span>
            <input type="password" name="password" id="password" placeholder="请输入密码" autocomplete="current-password">
        </div>
        <div class="row">
            <button id="submit" onclick="login()">提交</button>
        </div>
        <div class="row">
            <span style="font-size: 14px;">没有账号？<a href="blog_register.html" style="color: #007bff; text-decoration: none;">立即注册</a></span>
        </div>
    </div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
    // jQuery 加载失败的备用方案
    if (typeof jQuery === 'undefined') {
        document.write('<script src="blog-editormd/lib/jquery.min.js"><\/script>');
    }
</script>
<script src="js/common.js"></script>
<script>
    function login() {
        // 获取表单数据
        const username = $("#username").val().trim();
        const password = $("#password").val();
        
        // 前端验证
        if (!username) {
            showMessage("请输入用户名", "warning");
            $("#username").focus();
            return;
        }
        if (username.length < 3) {
            showMessage("用户名长度不能少于3位", "warning");
            $("#username").focus();
            return;
        }
        if (!password) {
            showMessage("请输入密码", "warning");
            $("#password").focus();
            return;
        }
        if (password.length < 6) {
            showMessage("密码长度不能少于6位", "warning");
            $("#password").focus();
            return;
        }
        
        // 显示加载状态
        showLoading("#submit", "登录中...");
        
        $.ajax({
            type: "post",
            url: "/user/login",
            contentType: "application/json",
            data: JSON.stringify({
                userName: username,
                password: password
            }),
            success: function(result) {
                hideLoading("#submit");
                if (result != null && result.code && result.code.toString() === "SUCCESS") {
                    // 登录成功
                    if (result.data && result.data.userId && result.data.token) {
                        localStorage.setItem("loginUserId", result.data.userId);
                        localStorage.setItem("userToken", result.data.token);
                        showMessage("登录成功！正在跳转...", "success");
                        setTimeout(() => {
                            location.href = "blog_list.html";
                        }, 1000);
                    } else {
                        showMessage("登录响应数据异常，请重试", "error");
                    }
                } else {
                    showMessage(result.errMsg || "登录失败，请重试", "error");
                }
            },
            error: function(xhr, status, error) {
                hideLoading("#submit");
                handleAjaxError(xhr, "登录失败，请重试");
            }
        });
    }
    
    // 页面加载完成后的处理
    $(document).ready(function() {
        // 检查是否已经登录
        const token = localStorage.getItem('userToken');
        if (token) {
            showMessage('您已经登录，正在跳转...', 'info');
            setTimeout(() => {
                location.href = 'blog_list.html';
            }, 1000);
            return;
        }
        
        // 聚焦到用户名输入框
        $('#username').focus();
    });
    
    // 回车键提交
    $(document).keypress(function(e) {
        if (e.which == 13) {
            login();
        }
    });
</script>
</body>

</html>